package com.shop.cereshop.app.controller.buyer;

import com.shop.cereshop.app.domain.buyer.BuyerSearchVO;
import com.shop.cereshop.app.service.buyer.CereBuyerSearchService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.utils.EmptyUtils;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @description: 客户历史搜索相关接口
 * @author zdd
 * @date 2025/10/21 14:19
 * @version 1.0
 */
@RestController
@RequestMapping("/buyer/search")
@Slf4j(topic = "BuyerSearchController")
@Api(value = "客户历史搜索相关接口", tags = "客户历史搜索相关接口")
public class BuyerSearchController {

    @Resource
    private CereBuyerSearchService buyerSearchService;

    @ApiOperation("查询搜索历史")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchType", value = "搜索类型（0或不传-全部,1-商品,2-店铺,3-购物车）", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class),
    })
    @GetMapping("/list")
    public Result<List<BuyerSearchVO>> getBuyerSearchList(HttpServletRequest request, Integer searchType) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (EmptyUtils.isEmpty(user)) {
            log.warn("用户未登录");
            return Result.success(Collections.emptyList());
        }
        return buyerSearchService.getBuyerSearchList(user.getBuyerUserId(), searchType);
    }

    @ApiOperation("猜你想搜")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchType", value = "搜索类型（0或不传-全部,1-商品,2-店铺,3-购物车）", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class),
    })
    @GetMapping("/suggest")
    public Result<List<BuyerSearchVO>> getBuyerSuggestedSearchList(HttpServletRequest request, Integer searchType) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (EmptyUtils.isEmpty(user)) {
            log.warn("用户未登录");
            return Result.success(Collections.emptyList());
        }
        return buyerSearchService.getUserHotSearch(user.getBuyerUserId(), searchType);
    }

    @ApiOperation("删除搜索记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchId", value = "搜索记录ID", required = false, paramType = "query", dataType = "Long", dataTypeClass = Long.class),
    })
    @DeleteMapping("/delete/{searchId}")
    public Result<Void> deleteBuyerSearch(HttpServletRequest request, @PathVariable Long searchId) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (EmptyUtils.isEmpty(user)) {
            return Result.failed(CoReturnFormat.USER_NOT_LOGIN);
        }
        return buyerSearchService.deleteBuyerSearch(user.getBuyerUserId(), searchId);
    }

    @ApiOperation("清空搜索记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchType", value = "搜索类型（0或不传-全部,1-商品,2-店铺,3-购物车）", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class),
    })
    @DeleteMapping("/deleteAll")
    public Result<Void> deleteBuyerAllSearch(HttpServletRequest request, Integer searchType) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (EmptyUtils.isEmpty(user)) {
            return Result.failed(CoReturnFormat.USER_NOT_LOGIN);
        }
        return buyerSearchService.deleteBuyerAllSearch(user.getBuyerUserId(), searchType);
    }

}
